package com.base.myproject.client.busi.ccode;

import java.util.Date;
import java.util.HashMap;

import org.jbpm.pvm.internal.model.ProcessModificationsImpl.AddedAction;

import com.base.myproject.client.busi.BaseStore;
import com.base.myproject.client.busi.BaseStoreEvent;
import com.base.myproject.client.busi.BaseStoreListener;
import com.base.myproject.client.busi.BO.BusiObject;
import com.base.myproject.client.jdbc.DataSet;
import com.base.myproject.client.jdbc.StoreInfo;
import com.base.myproject.client.system.BaseResource;
import com.base.myproject.client.system.Env;
import com.base.myproject.client.ui.BaseFormPanel;
import com.base.myproject.client.ui.celleditor.BaseComboBoxForm;
import com.base.myproject.client.ui.codenamemap.CodeNameMapFactory;
import com.extjs.gxt.ui.client.Style.Orientation;
import com.extjs.gxt.ui.client.data.BaseModelData;
import com.extjs.gxt.ui.client.data.ModelType;
import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.widget.Dialog;
import com.extjs.gxt.ui.client.widget.HorizontalPanel;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.DateField;
import com.extjs.gxt.ui.client.widget.form.Field;
import com.extjs.gxt.ui.client.widget.form.TextArea;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.form.Validator;
import com.extjs.gxt.ui.client.widget.layout.RowData;
import com.extjs.gxt.ui.client.widget.layout.RowLayout;
import com.extjs.gxt.ui.client.widget.layout.TableData;
import com.extjs.gxt.ui.client.widget.layout.TableLayout;
import com.google.gwt.core.client.GWT;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.AbstractImagePrototype;
import com.google.gwt.user.client.ui.Label;

public class EditorCcode_service extends Dialog implements Listener<BaseEvent>{
	BaseFormPanel w;

	TextField<String> ccode_t;
	TextField<String> cardno_t;
	TextField<String> icode_t;
	TextField<String> cname_t;
	TextField<String> password_t = new TextField<String>();
	TextField<String> passwordc_t = new TextField<String>();
	Button query = new Button("查询");
	HorizontalPanel h = new HorizontalPanel();
	public EditorCcode_service()
	{
		init();
	}
	void init()
	{
		GWT.log("init");
	}
	 protected void onRender(Element parent, int index) {  
		
			
		 super.onRender(parent, index);  
		 initmainpanel();
			
			setHeading("修改会员");
			setIcon(AbstractImagePrototype.create(BaseResource.INSTANCE
					.contact24()));
			setLayout(new RowLayout(Orientation.VERTICAL));

			setButtons(Dialog.YESNO);
//			getButtonById(Dialog.YES).setText("修改新会员");
//			getButtonById(Dialog.NO).setText("关闭");
			getButtonById(Dialog.YES).setIcon(
					AbstractImagePrototype
							.create(BaseResource.INSTANCE.save2_16()));

			getButtonById(Dialog.YES).addListener(Events.Select, this);
			getButtonById(Dialog.NO).addListener(Events.Select, this);
			getButtonById(Dialog.YES).setText("保存");
			getButtonById(Dialog.NO).setText("取消");
			add(h,new RowData(1, 0.2));
			add(w, new RowData(1, 0.8));

			setSize(607, 373);
			// d.setAutoHeight(true);
			// d.setAutoWidth(true);

			store._LoadComplete();
			store.addBaseStoreListener(BaseStoreEvent.EventType.LOAD_COMPLETE, new BaseStoreListener(){

				@Override
				public void BaseStoreEventHandle(BaseStoreEvent evt) {
					
					if(evt.getBasestore().getCount()>0)
					{
						w.setEnabled(true);
					}
					else if(evt.getBasestore().getCount()>1)
					{
						MessageBox.alert("错误", "异常，出现2个或以上相同卡号的会员", null);
						w.reset();
						w.setEnabled(false);
					}
					else{
						MessageBox.alert("错误", "未找到指定卡号的会员", null);
						w.reset();
						w.setEnabled(false);
					}
				}

				@Override
				public void onBrowserEvent(Event event) {
				
					
				}

			
				
			});
			
	 }
	 BaseStore<BusiObject> store;

		BaseStore<BusiObject> getStore() {
			store = new BaseStore<BusiObject>();
			// store.addBaseStoreListener( EventType.ROW_ADDED,
			// getBasestoreListener());

			ModelType type = new ModelType();
			type.setRoot("records");
			type.setRecordName("record");
			type.setTotalName("totle");
			type.addField("icode");
			type.addField("ccode");
			type.addField("cname");
			type.addField("s_cardno");
			type.addField("s_sex");
			type.addField("s_edu");
			type.addField("s_birid");
			type.addField("s_mobil");
			type.addField("s_idno");
			type.addField("s_mark");
			type.addField("password");

			type.addField("createbname");
			type.addField("createdate");
			type.addField("updatebname");
			type.addField("updatedate");
			type.addField("ccodetype");
			type.addField("rbcode");
			
			type.getField("s_cardno").setType(String.class);
			type.getField("s_sex").setType(Integer.class);

			type.getField("s_edu").setType(String.class);
			type.getField("s_birid").setType(Date.class);

			type.getField("s_mobil").setType(String.class);
			type.getField("s_idno").setType(String.class);
			type.getField("s_mark").setType(String.class);
			type.getField("password").setType(String.class);
			StoreInfo info = new StoreInfo();
			info.setTablename("ccode");
			info.setKeyname(new String[] { "icode" });
			store.setType(type);
			store.setStoreinfo(info);
			setdefault();
			HashMap hm = new HashMap();

			BusiObject modeldata = new BusiObject(hm);

			

			return store;
		}

	 private void setdefault() {

			DateTimeFormat activeFormat = DateTimeFormat
					.getFormat("yyyy-M-d H:mm:ss");
			activeFormat.format(new Date());

			store.addDefault("ccodetype", "1");
			store.addDefault("createbcode", Env.getEnvbcode());
			store.addDefault("createbname", Env.getEnvname());
			store.addDefault("createdate", activeFormat.format(new Date()));

		}
	 /***
		 * 主窗口
		 */
		void initmainpanel() {

			w = new BaseFormPanel(getStore(),false);
			w.setHeaderVisible(false);
			Label cardno_l = new Label("录入卡号进行查找:");
			cardno_l.setWidth("150");
			Label cname_l = new Label("名字:");
			cname_l.setWidth("50");
			Label sex_l = new Label("性别:");
			sex_l.setWidth("50");
			Label edu_l = new Label("学历:");
			Label bird_l = new Label("出生日期:");

			Label mobil_l = new Label("手机号:");
			Label idno_l = new Label("身份证号:");
			Label mark_l = new Label("备注:");
			

			cardno_t = new TextField<String>();
			cardno_t.setAllowBlank(false);
			//cardno_t.addListener(Events.Change, this);
			cname_t = new TextField<String>();
			cname_t.setAllowBlank(false);
			
//			passwordc_t.setAllowBlank(false);
//			password_t.setAllowBlank(false);
//			
			// TextField<String> sex_t = new TextField<String>();
			// BaseGridEditor sex_t= CodeNameMapFactory.aa();
			BaseComboBoxForm<BaseModelData> sex_t = CodeNameMapFactory
					.createSexInput_F();
			TextField<String> edu_t = new TextField<String>();
			// CodeNameMapFactory codenamemap = new CodeNameMapFactory();

			// BaseGridEditor<String> edu_t =codenamemap.createSexInput_G();

			// createBocdeInput_F
			DateField bird_t = new DateField();
			bird_t.setAutoWidth(true);

			bird_t.getPropertyEditor().setFormat(
					DateTimeFormat.getFormat("yyyy-M-d H:mm:ss"));
			TextField<String> mobil_t = new TextField<String>();
			
			mobil_t.setAllowBlank(false);
			TextField<String> idno_t = new TextField<String>();

			TextArea mark_t = new TextArea();
			mark_t.setSize(451, 40);
			//TextField<String> password_t = new TextField<String>();

//			password_t.setPassword(true);
//			passwordc_t.setPassword(true);
			// password_t.setToolTip("将光标放到此处，并由会员录入自己的密码");
			ccode_t = new TextField<String>();
			// ccode_t.setEnabled(false);
			ccode_t.setVisible(false);
			icode_t = new TextField<String>();
			icode_t.setVisible(false);

			//cardno_t.setName("s_cardno");
			cname_t.setName("cname");
			password_t.setName("password");

			sex_t.setName("s_sex");
			edu_t.setName("s_edu");
			bird_t.setName("s_birid");
			mobil_t.setName("s_mobil");
			idno_t.setName("s_idno");
			mark_t.setName("s_mark");
			ccode_t.setName("ccode");
			icode_t.setName("icode");
			//password_t.setName("password");

			TableLayout tl = new TableLayout(4);
			tl.setWidth("600");
			w.setLayout(tl);
			TableData td = new TableData();
			td.setColspan(1);
			// td.setWidth("2000");
			td.setMargin(100);
			td.setPadding(5);
			td.setRowspan(1);

			TableData td2 = new TableData();
			td2.setColspan(3);
			// td.setWidth("2000");
			td2.setMargin(100);
			td2.setPadding(5);
			td2.setRowspan(1);
			
			TableData td3 = new TableData();
			td3.setColspan(4);
			// td.setWidth("2000");
			td3.setMargin(100);
			td3.setPadding(5);
			td3.setRowspan(1);

			
			h.setBorders(true);
			h.add(cardno_l, td);
			h.add(cardno_t, td);
			
			h.add(query,td2);
			
			
			//w.add(h,td3);
			
			w.add(cname_l, td);
			w.add(cname_t, td);

			w.add(mobil_l, td);
			w.add(mobil_t, td);
			w.add(sex_l, td);
			w.add(sex_t, td);
			
//			w.add(new Label("密码："), td);
//			w.add(password_t, td);
//			w.add(new Label("重新录入密码："), td);
//			w.add(passwordc_t, td);
//			
			
			w.add(edu_l, td);
			w.add(edu_t, td);
			w.add(bird_l, td);
			w.add(bird_t, td);

			w.add(idno_l, td);
			w.add(idno_t, td);
			//w.add(new Label(""), td);
			//w.add(new Label(""), td);
			w.add(mark_l, td);
			w.add(mark_t, td2);
			
	
			
			
			w.add(ccode_t, td);
//			w.add(icode_t, td);
			
			
		
			w.inited();

			// w.setAutoHeight(true);
			// w.setAutoWidth(true);
			// w.setSize(661, 300);

			cardno_t.addListener(Events.Changed, this);
			cname_t.addListener(Events.Changed, this);
			query.addListener(Events.Select, this);
			// mobil_t.addListener(Events.Change, this);

			cardno_t.setValidateOnBlur(true);
			cname_t.setValidateOnBlur(true);
			
		}
		@Override
		public void handleEvent(BaseEvent be) {
			// TODO Auto-generated method stub

			if (be.getType().equals(Events.Select)) {
				if(be.getSource().equals(query))
				{
					String cardno = cardno_t.getValue();
					if(cardno!=null)
					{
					
					String sql = "select s_cardno,cname,s_sex ,s_edu,s_birid,s_mobil,s_idno,s_mark ,ccode,icode from ccode where s_cardno = '"+cardno+"'";
					store.getStoreinfo().setSql(sql);
					store.load();
					}
				}
				else if(be.getSource().equals(getButtonById(Dialog.YES)))
				{
					if(w.isEnabled() && w.isValid())
					{
						store.trySave();
					}
					else
					{
						MessageBox.alert("警告", "没有需要保存的信息或者数据录入有错误。", null);
					}
				}
				else if(be.getSource().equals(getButtonById(Dialog.NO)))
				{
					this.hide();
				}
					
			}
			else if (be.getType().equals(Events.Changed)) {
				 if (be.getSource().equals(cname_t)) {

					 if(store.getValue("s_cardno")==null)
					 {
						 MessageBox.alert("错误", "未能获得会员资料。", null);
						 return;
					 }
					final com.base.myproject.client.tools.GreetingServiceAsync greetingService = GWT
							.create(com.base.myproject.client.tools.GreetingService.class);
					greetingService.getDataSet(
							"select count(*) as c from ccode where cname='"
									+ cname_t.getValue() + "' and s_cardno !='"+store.getValue("s_cardno")+"'",
							new AsyncCallback<DataSet>() {

								@Override
								public void onFailure(Throwable caught) {

								}

								@Override
								public void onSuccess(DataSet result) {

									if (Integer.parseInt(result.getValue(0, "c")) > 0) {
										Validator v = new Validator() {

											@Override
											public String validate(Field<?> field,
													String value) {

												return "已经存在相同名字的用户";
											}
										};

										cname_t.setValidator(v);
									} else {
										cname_t.setValidator(null);

									}

									cname_t.validate();
								}

							});

				}
			}
		}
}
